package top.kaoshanji.p2ch08a;

import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.tree.ClassNode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

/**
 * 代码清单-p2ch08a-266页a：使用树形API计算方法格式
 * author kaoshanji
 * ^_^
 * create time 2025/1/22 19:49
 */
public class ASMExample02 {

    private static final Logger logger = LogManager.getLogger(ASMExample02.class);

    public static void main(String[] args) throws IOException {
        logger.info("...代码清单-p2ch08a-266页a：使用树形API计算方法格式...");

        ASMExample02 example = new ASMExample02();
        int count = example.count("java.lang.String");
        logger.info("...count:{}...", count);
    }

    public int count(String className) throws IOException {
        ClassReader reader = new ClassReader(className);
        ClassNode cn = new ClassNode();
        reader.accept(cn, 0);
        return cn.methods != null ? cn.methods.size() : 0;
    }

}
